﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace laohan.Common.Common
{
    public partial class API
    {
        /// <summary>
        /// 
        /// </summary>
        public static List<NetWorkInfo> GetNetworkInfo()
        {
            var list = new List<NetWorkInfo>();

            var nics = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface adapter in nics)
            {
                var net = new NetWorkInfo();

                net.Id = adapter.Id;
                net.Name = adapter.Name;

                PhysicalAddress physicalAddress = adapter.GetPhysicalAddress();

                byte[] bytes = physicalAddress.GetAddressBytes();

                StringBuilder macAddresss = new StringBuilder();

                foreach (byte b in bytes)
                {
                    macAddresss.AppendFormat("{0}:", b.ToString("X2"));
                }

                if (macAddresss.Length > 0)
                {
                    macAddresss.Remove(macAddresss.Length - 1, 1);
                }

                net.MacAddress = macAddresss.ToString();
                net.Speed = adapter.Speed;
                net.NetworkInterfaceType = (int)adapter.NetworkInterfaceType;
                net.supportsMulticast = adapter.SupportsMulticast.ToString();
                net.operationalStatus = adapter.OperationalStatus.ToString();
                net.Description = adapter.Description;

                var ip = adapter.GetIPProperties();//获取以太网卡网络接口信息

                var gateways = ip.GatewayAddresses;

                string gateway = string.Empty;

                foreach (var gateWay in gateways)
                {
                    gateway = gateWay.Address.ToString() + ",";
                }

                if (gateway.Length > 0)
                {
                    gateway.Remove(gateway.Length - 1, 1);
                }

                net.Gateway = gateway;

                IPAddressCollection dnsAddresses = ip.DnsAddresses;

                string dns1 = string.Empty;
                string dns2 = string.Empty;

                int index = 0;

                foreach (var item in dnsAddresses)
                {
                    if (item.AddressFamily == AddressFamily.InterNetwork)
                    {
                        if (index == 0)
                        {
                            dns1 = item.ToString();
                        }
                        else if (index == 1)
                        {
                            dns2 = item.ToString();
                        }

                        index++;
                    }
                }

                net.Dns1 = dns1;
                net.Dns2 = dns2;

                var ipCollection = ip.UnicastAddresses;//获取单播地址集

                foreach (UnicastIPAddressInformation ipadd in ipCollection)
                {
                    if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)//获取ip
                    {
                        if (net.IpInfo == null)
                        {
                            net.IpInfo = new List<IpInfo>();
                        }

                        net.IpInfo.Add(new IpInfo()
                        {
                            IP = ipadd.Address.ToString(),
                            IPMask = ipadd.IPv4Mask.ToString()
                        });
                    }
                    else if (ipadd.Address.AddressFamily == AddressFamily.InterNetworkV6)//本机IPV6 地址
                    {

                    }
                }

                list.Add(net);
            }

            return list;
        }

        /// <summary>
        /// 获取本机所有ip地址
        /// </summary>
        /// <param name="netType">"InterNetwork":ipv4地址，"InterNetworkV6":ipv6地址</param>
        /// <returns>ip地址集合</returns>
        public static List<string> GetLocalIpAddress(string netType)
        {
            string hostName = Dns.GetHostName();                    //获取主机名称
            IPAddress[] addresses = Dns.GetHostAddresses(hostName); //解析主机IP地址

            List<string> IPList = new List<string>();

            if (netType == string.Empty)
            {
                for (int i = 0; i < addresses.Length; i++)
                {
                    IPList.Add(addresses[i].ToString());
                }
            }
            else
            {
                //AddressFamily.InterNetwork表示此IP为IPv4,
                //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                for (int i = 0; i < addresses.Length; i++)
                {
                    if (addresses[i].AddressFamily.ToString() == netType)
                    {
                        IPList.Add(addresses[i].ToString());
                    }
                }
            }
            return IPList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<string> GetLocalIpAddressAll()
        {
            return GetLocalIpAddress(string.Empty);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<string> GetLocalIpAddressV4()
        {
            return GetLocalIpAddress("InterNetwork");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<string> GetLocalIpAddressV6()
        {
            return GetLocalIpAddress("InterNetworkV6");
        }
    }
}